package com.spzx.payment.service.impl;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.spzx.common.core.domain.R;
import com.spzx.order.api.RemoteOrderService;
import com.spzx.order.api.domain.OrderInfo;
import com.spzx.order.api.domain.OrderItem;
import com.spzx.payment.service.AlipayService;
import com.spzx.payment.service.PaymentService;
import com.spzx.rabbit.constant.MqConst;
import com.spzx.rabbit.service.RabbitService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class AlipayServiceImpl implements AlipayService {

    @Autowired
    RemoteOrderService remoteOrderService;

    @Autowired
    PaymentService paymentService;

    @Autowired
    RabbitService rabbitService;

    String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    String appId = "9021000141677185";
    String spzxprivateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCaHpy0WbMomv7Ae2w77LD2VUkAuQCJkd94tvMtFYY8xvLaW/llmQt6rHAKLoWdgq31Qp4nyIYFpWSxChKQSgrJKui/pDR6JiXzCcVexu2na0mlq2OTGzfx5VZ+AbMgUjiAOdl2H63qg89Rgr1Y8McP8voVuftVQ5KOidh6kmeLaCeM3k/hitkwxILP9LowXwTdcN/+5veW9JLszwO6kCWuVTa6kXfrEKwtBJZxkI6vh//Qa5SN+vl4cfKhjqj5DLyoY1Uip1NudrJgnxUuC+z/q53siem1RsOLWsEL0b9BGAUnrIqtDNbY3UJCTsBqGbjuR+x7TJrVwW8ZwHjTWF7jAgMBAAECggEADF1sYSPZNebJjZPZaS1iPUE2bDJrkp37eenjtLhHXtTvYwmCBt6yhxMSoh59Zde4MTc5DSQPbKCKZZHTT3RBTUZ+CfIYq7wny0n7ZdE7+/Slof8QLBtA781glRd50ZwHIHKDufBsu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PJs6bP2vtlaTjBVBnY=";
    String format = "json";
    String charset = "utf-8";
    String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApdB1QQsaBHCmYNt51v+ZG6FBok0t12kCprwVLeeR1H6pMyB1RxexDkndec045aYAjH5Su2VF7siv5EeTYVoq5vs+jA3OU5AyEAknuCcj0QJwA6c0k35eU+t/Da9eRB5zF118K7z+eOIPqJHYynlFAixg6w7ks/DZkU6KqDX572CGySHzLCFN+Ju6wb69Ov/MS8R5qEDxQy16vPqEdCItNiz1b26HLA1O2p+13bWGOM59KBPGvQ5O0c6bRvDqBaI7c1SJeu+YcrWwEfZ8KtXLG45Ya3B0whehfhkhr+BrxmYVqAeeowu6QA3L/xWAmp9CEJdJMqVQeMFL1HbzoQXr3wIDAQAB";

    @SneakyThrows
    @Override
    public String submitAlipay(String orderNo) {

        // 调用支付宝sdk, 生成跳转form

        // 1. 创建支付宝客户端实例
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,appId,spzxprivateKey,format,charset,alipayPublicKey,"RSA2");

        // 2. 拼接请求参数对象
        AlipayTradeWapPayRequest alipayTradeWapPayRequest = new AlipayTradeWapPayRequest();
        Map<String, Object> map = new HashMap<>();
        map.put("out_trade_no",orderNo);
        R<OrderInfo> orderByOrderNoR = remoteOrderService.getOrderByOrderNo(orderNo, "inner");
        OrderInfo orderInfo = orderByOrderNoR.getData();
        map.put("total_amount",orderInfo.getTotalAmount());
        String title = "";
        for (OrderItem orderItem : orderInfo.getOrderItemList()) {
            title = title + "," + orderItem.getSkuName();
        }
        map.put("subject",title); // 商品标题
        map.put("product_code","QUICK_WAP_WAY");
        alipayTradeWapPayRequest.setBizContent(JSON.toJSONString(map));

        // 设置特殊参数
        alipayTradeWapPayRequest.setReturnUrl("http://192.168.222.128/#/pages/money/paySuccess");// spzx的重定向回朓页面
        alipayTradeWapPayRequest.setNotifyUrl("http://f5mfs6.natappfree.cc/payment/alipay/callback");// spzx的接口

        // 3. 发送请求
        AlipayTradeWapPayResponse alipayTradeWapPayResponse = alipayClient.pageExecute(alipayTradeWapPayRequest);
        String body = alipayTradeWapPayResponse.getBody();

        return body;
    }

    @Override
    public void  callback(Map<String, String> paramsMap) {
        String out_trade_no = paramsMap.get("out_trade_no"); // 支付成功返回订单号
        System.out.println("支付宝被阿里回调业务");
        System.out.println("更新支付状态");
        // 更新支付状态

        // 通知订单， 修改订单状态
        rabbitService.sendMessage(MqConst.EXCHANGE_PAYMENT_PAY, MqConst.ROUTING_PAYMENT_PAY,out_trade_no);
    }

   /* @Autowired
    RemoteOrderService remoteOrderService;*/

   /* @Autowired
    PaymentService paymentService;*/

   /* @Autowired
    RabbitService rabbitService;*/
/*

    String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    String appId = "9021000141677185";
    String spzxPrivateKey ="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCaHpy0WbMomv7Ae2w77LD2VUkAuQCJkd94tvMtFYY8xvLaW/llmQt6rHAKLoWdgq31Qp4nyIYFpWSxChKQSgrJKui/pDR6JiXzCcVexu2na0mlq2OTGzfx5VZ+AbMgUjiAOdl2H63qg89Rgr1Y8McP8voVuftVQ5KOidh6kmeLaCeM3k/hitkwxILP9LowXwTdcN/+5veW9JLszwO6kCWuVTa6kXfrEKwtBJZxkI6vh//Qa5SN+vl4cfKhjqj5DLyoY1Uip1NudrJgnxUuC+z/q53siem1RsOLWsEL0b9BGAUnrIqtDNbY3UJCTsBqGbjuR+x7TJrVwW8ZwHjTWF7jAgMBAAECggEADF1sYSPZNebJjZPZaS1iPUE2bDJrkp37eenjtLhHXtTvYwmCBt6yhxMSoh59Zde4MTc5DSQPbKCKZZHTT3RBTUZ+CfIYq7wny0n7ZdE7+/Slof8QLBtA781glRd50ZwHIHKDufBsuaX9miKNInee4O6zlc6oFX0oYvs4PQ1F9W8zYKL+oEI89Ifx1qRVz0o+Tkl5Yr8CV2Rjola6XmC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QEFAAOCAQ8AMIIBCgKCAQEApdB1QQsaBHCmYNt51v+ZG6FBok0t12kCprwVLeeR1H6pMyB1RxexDkndec045aYAjH5Su2VF7siv5EeTYVoq5vs+jA3OU5AyEAknuCcj0QJwA6c0k35eU+t/Da9eRB5zF118K7z+eOIPqJHYynlFAixg6w7ks/DZkU6KqDX572CGySHzLCFN+Ju6wb69Ov/MS8R5qEDxQy16vPqEdCItNiz1b26HLA1O2p+13bWGOM59KBPGvQ5O0c6bRvDqBaI7c1SJeu+YcrWwEfZ8KtXLG45Ya3B0whehfhkhr+BrxmYVqAeeowu6QA3L/xWAmp9CEJdJMqVQeMFL1HbzoQXr3wIDAQAB";
*/

    /*@SneakyThrows
    @Override
    public String submitAlipay(String orderNo) {
        // 调用支付宝sdk，生成跳转form


        // 1 创建支付宝客户端实例
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,appId,spzxPrivateKey,format,charset,alipayPublicKey,"RSA2");


        // 2 拼接请求参数对象
        AlipayTradeWapPayRequest alipayTradeWapPayRequest = new AlipayTradeWapPayRequest();
        Map<String,Object> map = new HashMap<>();
        map.put("out_trade_no",orderNo);
        R<OrderInfo> orderByOrderNoR = remoteOrderService.getOrderByOrderNo(orderNo,"inner");
        OrderInfo orderInfo = orderByOrderNoR.getData();
        map.put("total_amount",orderInfo.getTotalAmount());
        String title = "";
        for (OrderItem orderItem : orderInfo.getOrderItemList()) {
            title = title + "," + orderItem.getSkuName();
        }
        map.put("subject",title);// 商品标题
        map.put("product_code","QUICK_WAP_WAY");
        alipayTradeWapPayRequest.setBizContent(JSON.toJSONString(map));

        // 设置特殊参数
        alipayTradeWapPayRequest.setReturnUrl("http://192.168.222.128/#/pages/money/paySuccess");// spzx的重定向回朓页面
        alipayTradeWapPayRequest.setNotifyUrl("http://8vrsyg.natappfree.cc/payment/alipay/callback");// spzx的接口

        // 3 发送请求
        AlipayTradeWapPayResponse alipayTradeWapPayResponse = alipayClient.pageExecute(alipayTradeWapPayRequest);
        String body = alipayTradeWapPayResponse.getBody();

        // 生成支付信息

        return body;
    }*/

   /* @Override
    public void callback(Map<String, String> paramsMap) {
        String out_trade_no = paramsMap.get("out_trade_no");
        System.out.println("支付宝被阿里回调业务");
        System.out.println("更新支付状态");
        // 更新支付状态

        // 通知订单，修改订单状态
        rabbitService.sendMessage(MqConst.EXCHANGE_PAYMENT_PAY,MqConst.ROUTING_PAYMENT_PAY,out_trade_no);
    }*/


}
